'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const f = require('../nodes/identity.cjs'),
  E = require('../nodes/Scalar.cjs'),
  j = require('./stringify.cjs'),
  u = require('./stringifyComment.cjs');
function P({ key: l, value: n }, e, y, h) {
  const {
    allNullValues: C,
    doc: B,
    indent: $,
    indentStep: q,
    options: { commentString: d, indentSeq: D, simpleKeys: c },
  } = e;
  let t = (f.isNode(l) && l.comment) || null;
  if (c) {
    if (t) throw new Error('With simple keys, key nodes cannot have comments');
    if (f.isCollection(l)) {
      const m = 'With simple keys, collection cannot be used as a key value';
      throw new Error(m);
    }
  }
  let s =
    !c &&
    (!l ||
      (t && n == null && !e.inFlow) ||
      f.isCollection(l) ||
      (f.isScalar(l)
        ? l.type === E.Scalar.BLOCK_FOLDED || l.type === E.Scalar.BLOCK_LITERAL
        : typeof l == 'object'));
  e = Object.assign({}, e, {
    allNullValues: !1,
    implicitKey: !s && (c || !C),
    indent: $ + q,
  });
  let p = !1,
    g = !1,
    i = j.stringify(
      l,
      e,
      () => (p = !0),
      () => (g = !0),
    );
  if (!s && !e.inFlow && i.length > 1024) {
    if (c)
      throw new Error(
        'With simple keys, single line scalar must not span more than 1024 characters',
      );
    s = !0;
  }
  if (e.inFlow) {
    if (C || n == null) return p && y && y(), i === '' ? '?' : s ? `? ${i}` : i;
  } else if ((C && !c) || (n == null && s))
    return (
      (i = `? ${i}`),
      t && !p ? (i += u.lineComment(i, e.indent, d(t))) : g && h && h(),
      i
    );
  p && (t = null),
    s
      ? (t && (i += u.lineComment(i, e.indent, d(t))),
        (i = `? ${i}
${$}:`))
      : ((i = `${i}:`), t && (i += u.lineComment(i, e.indent, d(t))));
  let S, w, b;
  f.isNode(n)
    ? ((S = !!n.spaceBefore), (w = n.commentBefore), (b = n.comment))
    : ((S = !1),
      (w = null),
      (b = null),
      n && typeof n == 'object' && (n = B.createNode(n))),
    (e.implicitKey = !1),
    !s && !t && f.isScalar(n) && (e.indentAtStart = i.length + 1),
    (g = !1),
    !D &&
      q.length >= 2 &&
      !e.inFlow &&
      !s &&
      f.isSeq(n) &&
      !n.flow &&
      !n.tag &&
      !n.anchor &&
      (e.indent = e.indent.substring(2));
  let F = !1;
  const o = j.stringify(
    n,
    e,
    () => (F = !0),
    () => (g = !0),
  );
  let r = ' ';
  if (t || S || w) {
    if (
      ((r = S
        ? `
`
        : ''),
      w)
    ) {
      const m = d(w);
      r += `
${u.indentComment(m, e.indent)}`;
    }
    o === '' && !e.inFlow
      ? r ===
          `
` &&
        (r = `

`)
      : (r += `
${e.indent}`);
  } else if (!s && f.isCollection(n)) {
    const m = o[0],
      O = o.indexOf(`
`),
      L = O !== -1,
      K = e.inFlow ?? n.flow ?? n.items.length === 0;
    if (L || !K) {
      let N = !1;
      if (L && (m === '&' || m === '!')) {
        let a = o.indexOf(' ');
        m === '&' &&
          a !== -1 &&
          a < O &&
          o[a + 1] === '!' &&
          (a = o.indexOf(' ', a + 1)),
          (a === -1 || O < a) && (N = !0);
      }
      N ||
        (r = `
${e.indent}`);
    }
  } else
    (o === '' ||
      o[0] ===
        `
`) &&
      (r = '');
  return (
    (i += r + o),
    e.inFlow
      ? F && y && y()
      : b && !F
        ? (i += u.lineComment(i, e.indent, d(b)))
        : g && h && h(),
    i
  );
}
exports.stringifyPair = P;
